package controller

import (
	"encoding/json"
	"fmt"
	"sort"
	"time"

	tableBank "gitee.com/klein-x/biz/model/mysql/table/bank"
	tableUser "gitee.com/klein-x/biz/model/mysql/table/user"
	"gitee.com/klein-x/biz/module/db/mysql"
	"gitee.com/klein-x/biz/xutils"
	"github.com/cloudwego/hertz/pkg/common/hlog"
)

const (
	BANK_DB_ERROR               = 9900 //数据库通信发生错误
	BANK_INVAILD_QUESTION       = 2001 //无效的question(题库有问题)
	BANK_INVAILD_QUESTION_ID    = 2002
	BANK_INVAILD_PAPER_ID       = 2003 //无效的试卷id
	BANK_INVAILD_ANSWER_CARD_ID = 2004 // 无效的答题卡id
	BANK_INVAILD_CHAPTER_ID     = 2005
)

// Question 表示题目详细信息，包含题目、选项、解释和背景
func GetQuestionInfo(questionID string) (*QuestionInfo, error) {
	var info QuestionInfo

	// 从数据库中查询题目
	question, err := tableBank.QuestionGetByKey(questionID)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	// 数据库中没有记录
	if question == nil {
		return nil, nil
	}

	switch question.QuestionType {
	case "1":
		info.Type = "SCQ" // 单选
	case "2":
		info.Type = "MCQ" // 多选
	case "3":
		info.Type = "T/F" // 判断
	}

	if question.BackgroundID != nil {
		bk, err := tableBank.BackGroundGetByKey(*question.BackgroundID)
		if err != nil {
			return nil, xutils.Error(BANK_DB_ERROR, err)
		}

		if bk == nil {
			return nil, xutils.Error(BANK_INVAILD_QUESTION, fmt.Sprintf("无效的backgroundID:%v", question.BackgroundID))
		}

		info.Background = json.RawMessage(bk.BackgroundText)
	}
	info.Question = json.RawMessage(question.QuestionText)
	info.Options = json.RawMessage(question.OptionText)
	info.Correct = question.CorrectAnswer
	info.Explanation = json.RawMessage(question.Explanation)
	return &info, nil
}

func GetUserChapterInfos(uuid string) ([]*ChapterSummaryInfo, error) {

	// 获取用户资产
	assets, err := tableUser.AssetsGetByUser(uuid)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	// 没有查询到资产
	if assets == nil {
		return nil, nil
	}

	var chapterSummaryList []*ChapterSummaryInfo
	for _, asset := range *assets {
		chapter, err := tableBank.ChapterGetByUUID(asset.ChapterID)
		if err != nil {
			return nil, xutils.Error(BANK_DB_ERROR, err)
		}

		if chapter == nil {
			// log 有无效资产
			continue
		}
		var summaryInfo ChapterSummaryInfo
		summaryInfo.ChapterID = chapter.ID
		summaryInfo.Title = chapter.Title
		summaryInfo.EndTime = time.Unix(int64(asset.EndTime), 0).Format("2006-01-02 15:04:05")

		chapterSummaryList = append(chapterSummaryList, &summaryInfo)
	}
	return chapterSummaryList, nil
}

func GetChapterInfos(rootID string) ([]*ChapterInfo, error) {
	var chapterList []*ChapterInfo

	chNode, err := tableBank.ChapterGetByUUID(rootID)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	if chNode == nil {
		return nil, nil
	}

	chlist, err := tableBank.ChapterGetByAncestors(rootID)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	// 没有找到
	if chlist == nil {
		return nil, nil
	}

	chapterMap := make(map[string]*ChapterInfo)

	// 首先，将所有章节转换为ChapterList并存储到map中
	for _, chapter := range *chlist {
		// 初始化ChapterList结构
		node := ChapterInfo{
			Title:       chapter.Title,
			Description: chapter.Description,
			Status:      chapter.Status,
			SortOrder:   chapter.SortOrder,
			PaperId:     chapter.PaperID,
			Child:       []*ChapterInfo{},
		}
		chapterMap[chapter.ID] = &node
	}

	rootNode := &ChapterInfo{
		Title:       chNode.Title,
		Description: chNode.Description,
		Status:      chNode.Status,
		SortOrder:   chNode.SortOrder,
		PaperId:     chNode.PaperID,
		Child:       []*ChapterInfo{},
	}
	chapterList = append(chapterList, rootNode)
	hlog.Debugf("%v", chapterList)
	for _, ch := range *chlist {
		if ch.ParentID == rootID { // 根节点
			rootNode.Child = append(rootNode.Child, chapterMap[ch.ID])
			hlog.Debugf("app %v, %v", rootNode.Title, chapterMap[ch.ID].Title)
		} else {
			// 获取父章节的ChapterList并添加当前章节到其Child列表
			if parentChapter, exists := chapterMap[ch.ParentID]; exists {
				parentChapter.Child = append(parentChapter.Child, chapterMap[ch.ID])
				hlog.Debugf("app %v, %v", parentChapter.Title, chapterMap[ch.ID].Title)
			}
		}
	}
	return chapterList, nil
}

func GetExamInfo(uuid string, paperId string) (*ExamInfo, error) {
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	if paper == nil {
		return nil, xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	card, err := tableUser.AnswerCardGetByUserAndPaper(uuid, paperId)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	var info ExamInfo

	info.Paper.PaperId = paper.PaperID
	info.Paper.Title = paper.Title
	info.Paper.Description = paper.Description
	info.Paper.TotalScore = paper.TotalScore
	info.Paper.PassScore = paper.PassingScore

	// 试题信息
	for _, it := range paper.QuestionScores {
		info.Paper.Questions = append(info.Paper.Questions, it.ID)
	}
	if card != nil {
		info.AnswerCard.TotalQuestions = card.TotalQuestions
		info.AnswerCard.CorrectQuestions = card.CorrectQuestions
		info.AnswerCard.WrongQuestions = card.WrongQuestions
		info.AnswerCard.UnanswerQuestions = card.UnanswerQuestions
	} else {
		info.AnswerCard.TotalQuestions = (uint)(len(paper.QuestionScores))
		info.AnswerCard.CorrectQuestions = 0
		info.AnswerCard.WrongQuestions = 0
		info.AnswerCard.UnanswerQuestions = (uint)(len(paper.QuestionScores))
	}
	return &info, nil
}

func ResetUserExam(uuid string, paperId string) error {
	// 检测paper信息是否存在
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if paper == nil {
		return xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	//  从Paper构建答题卡的 AnscardId
	var answers = make(map[string][]string)
	for _, ques := range paper.QuestionScores {
		answers[ques.ID] = nil
	}

	// 制作一张空的答题卡
	card := tableUser.AnswerCardDefine(uuid, paperId)
	card.Answers = answers
	card.TotalQuestions = uint(len(answers))
	card.CorrectQuestions = 0
	card.WrongQuestions = 0
	card.UnanswerQuestions = uint(len(answers))

	// 获取之前的AnswerCard
	oldCard, err := tableUser.AnswerCardGetByUserAndPaper(uuid, paperId)
	if err != nil { // 没有记录不会返回错误， 所以这里肯定是其他错误
		return xutils.Error(BANK_DB_ERROR, err)
	}

	// 更新答题卡表
	if oldCard == nil {
		if err := tableUser.AnswerCardInsert(card); err != nil {
			return xutils.Error(BANK_DB_ERROR, err)
		}
		// 若AnswerCard为空则 答题卡的子表没数据
	} else {
		card.CardID = oldCard.CardID
		mysqlTrans := mysql.BeginTran()
		if err := tableUser.AnswerCardUpdate(card); err != nil {
			mysqlTrans.Rollback()
			return xutils.Error(BANK_DB_ERROR, err)
		}
		// 清理答题卡错误列表
		if err := tableUser.WrongQuestionResetByAnswerCardID(card.CardID); err != nil {
			mysqlTrans.Rollback()
			return xutils.Error(BANK_DB_ERROR, err)
		}
		mysqlTrans.Commit()
	}
	return nil
}

func BeginUserExam(uuid string, paperId string) (string, error) {
	// 检测paper信息是否存在
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return "", xutils.Error(BANK_DB_ERROR, err)
	}
	if paper == nil {
		return "", xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	// 获取答题卡信息
	answerCard, err := tableUser.AnswerCardGetByUserAndPaper(uuid, paperId)
	if err != nil {
		return "", xutils.Error(BANK_DB_ERROR, err)
	}

	if answerCard == nil {
		ResetUserExam(uuid, paperId) //如果没有答题卡,就制作一张答题卡
		if len(paper.QuestionScores) == 0 {
			return "", nil
		}
		return paper.QuestionScores[0].ID, nil // 并返回第一个试题的id
	} else { // 有答题卡就先检测题目是否对应的上， 以试卷为准, 多余的答题记录删除掉
		answerCardNeedUpdate := false
		if len(paper.QuestionScores) != len(answerCard.Answers) {
			answerCardNeedUpdate = true
		}
		// 数量相等的情况下判断每个项的值是否相等

		// 如果题目中有但是答题记录中没有， 说明记录中有不匹配的, 同时给答题记录中添加题目的答题记录
		for _, ques := range paper.QuestionScores {
			if _, exists := answerCard.Answers[ques.ID]; !exists {
				answerCardNeedUpdate = true // note: 这里要根据题目修改的频繁度来设计逻辑，很明显，有修改的可能性远远要小于查询的，所以不在这里做map的修改
			}
		}

		if answerCardNeedUpdate { //修正Answercard
			newAnsCard := make(map[string][]string)
			// 如果需要更新 那么就说明记录肯定不匹配了
			// 重新构建用户答案
			var Total, Wrong, UnAnswer uint = 0, 0, 0
			var WrongIds []string
			for _, ques := range paper.QuestionScores {
				if _, exists := answerCard.Answers[ques.ID]; exists {
					// 从数据库中查询这道题对应的question, 如果题有问题就报错
					quess, err := tableBank.QuestionGetByKey(ques.ID)
					if err != nil {
						return "", xutils.Error(BANK_DB_ERROR, err)
					}
					if quess == nil {
						return "", xutils.Error(BANK_INVAILD_QUESTION_ID, fmt.Sprintf("question error,%s not found", ques.ID))
					}
					// 如果新的答题卡中已经有了这道题的答案就跳过  --- 主要处理paper中 试题重复问题
					if _, exist := newAnsCard[ques.ID]; exist {
						continue
					} else { // 如果没有则将这道题加入到新的答题卡中
						newAnsCard[ques.ID] = answerCard.Answers[ques.ID]
					}

					Total++

					// 如果新答题卡的ans值是nil 或者新答题的ans数组长度为0 那么这道题是没有做过的
					if newAnsCard[ques.ID] == nil || len(newAnsCard[ques.ID]) == 0 {
						UnAnswer++
						continue
					}
					// 如果答题卡记录的值和标准答案不匹配,则记录错误
					if len(quess.CorrectAnswer) != len(answerCard.Answers[ques.ID]) {
						Wrong++
						WrongIds = append(WrongIds, answerCard.CardID)
					} else {
						sour := quess.CorrectAnswer
						dest := answerCard.Answers[ques.ID]
						sort.Strings(sour)
						sort.Strings(dest)

						for i := range sour {
							fmt.Println(i)
							if sour[i] != dest[i] {
								Wrong++
								WrongIds = append(WrongIds, answerCard.CardID)
								break
							}
						}
					}
				} else {
					newAnsCard[ques.ID] = []string{}
					UnAnswer++
					Total++
				}
			}

			// 重新构建答题信息
			answerCard.Answers = newAnsCard
			answerCard.TotalQuestions = Total
			answerCard.UnanswerQuestions = UnAnswer
			answerCard.WrongQuestions = Wrong
			answerCard.CorrectQuestions = answerCard.TotalQuestions - UnAnswer - Wrong
			fmt.Printf("total:%d, un:%d, wr:%d, co:%d\n", answerCard.TotalQuestions, answerCard.UnanswerQuestions, answerCard.WrongQuestions, answerCard.CorrectQuestions)
			//更新答题卡信息
			if err := tableUser.AnswerCardUpdate(answerCard); err != nil {
				return "", xutils.Error(BANK_DB_ERROR, err)
			}
			for _, ids := range WrongIds {
				if err := tableUser.WrongQuestionResetByAnswerCardID(ids); err != nil {
					return "", xutils.Error(BANK_DB_ERROR, err)
				}
			}
		}

		// 上面保证了Answer的正确性 则返回第一个没有答题记录的项就可以了
		for _, ques := range paper.QuestionScores {
			if _, exists := answerCard.Answers[ques.ID]; exists {
				if answerCard.Answers[ques.ID] == nil {
					return ques.ID, nil
				}
			}
		}
		if len(paper.QuestionScores) == 0 {
			return "", nil
		}
		return paper.QuestionScores[0].ID, nil
	}
}

func SetUserAnswer(uuid string, paperId string, questionId string, answer []string) error {
	// 获取paper表
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if paper == nil {
		return xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	// 获取Answer表
	ansCard, err := tableUser.AnswerCardGetByUserAndPaper(uuid, paperId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}
	if ansCard == nil {
		return xutils.Error(BANK_INVAILD_ANSWER_CARD_ID, "无效的答题卡ID")
	}

	ques, err := tableBank.QuestionGetByKey(questionId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if ques == nil {
		return xutils.Error(BANK_INVAILD_QUESTION_ID, "无效试题ID")
	}

	isUnAnswer, isUpdate, isNewWrong, isOldWrong := false, false, false, false

	// 查找答题卡中是否有当前记录
	if _, exists := ansCard.Answers[questionId]; !exists {
		// 如果答题卡中没记录, 说明BeginTrans阶段出了问题或者传值有问题
		return fmt.Errorf("答题卡中没有%s此条记录", questionId)
	}

	if ansCard.Answers[questionId] == nil { //说明之前此题未做过, 没有答案
		ansCard.Answers[questionId] = answer
		isUpdate = true
		isUnAnswer = true
		destStrs := answer
		corrStrs := ques.CorrectAnswer
		sort.Strings(destStrs)
		sort.Strings(corrStrs)
		isNewWrong = len(corrStrs) != len(destStrs)
		if !isNewWrong {
			for i := range corrStrs {
				if corrStrs[i] != destStrs[i] {
					isNewWrong = true
					break
				}
			}
		}
	} else { //说明之前这道题有过答案，属于重新作答
		sourStrs := ansCard.Answers[questionId]
		destStrs := answer
		corrStrs := ques.CorrectAnswer
		sort.Strings(sourStrs)
		sort.Strings(destStrs)
		sort.Strings(corrStrs)

		isUnAnswer = false
		isUpdate = len(sourStrs) != len(destStrs)
		if !isUpdate {
			for i := range sourStrs {
				if sourStrs[i] != destStrs[i] {
					isUpdate = true
					break
				}
			}
		}

		isNewWrong = len(corrStrs) != len(destStrs)
		if !isNewWrong {
			for i := range corrStrs {
				if corrStrs[i] != destStrs[i] {
					isNewWrong = true
					break
				}
			}
		}
		isOldWrong = len(corrStrs) != len(sourStrs)
		if !isOldWrong {
			for i := range corrStrs {
				if corrStrs[i] != sourStrs[i] {
					isOldWrong = true
					break
				}
			}
		}
	}

	if isUpdate { // 如果需要更新， 说明这道题的答案肯定更改了 要么从无到有， 要么从正确到错误， 要么从错误到正确
		ansCard.Answers[questionId] = answer
		if isUnAnswer { // 如果这道题是未做过的题,那么做了之后 总题数不变, 未做题减一
			ansCard.UnanswerQuestions--
			if isNewWrong {
				ansCard.WrongQuestions++
			} else {
				ansCard.CorrectQuestions++
			}
		} else { //如果是做过的题，那么总题数和未做题数不变
			if isOldWrong && !isNewWrong { // 之前做的是错的 现在改对了
				ansCard.CorrectQuestions++
				ansCard.WrongQuestions--
			} else if !isOldWrong && isNewWrong { // 之前是对的 现在改错了
				ansCard.CorrectQuestions--
				ansCard.WrongQuestions++
			} else {
				_ = "之前是对的，现在还是对的和之前是错的，现在还是错的，这两个不修改"
			}
		}

		tx := mysql.BeginTran()
		if tx.Error != nil {
			return xutils.Error(BANK_DB_ERROR, err)
		}
		// 写入AnswerCard表
		if err := tableUser.AnswerCardUpdate(ansCard); err != nil {
			tx.Rollback()
			return xutils.Error(BANK_DB_ERROR, err)
		}
		if err := tableUser.WrongQuestionSet(uuid, paperId, questionId, ansCard.CardID, isNewWrong); err != nil {
			tx.Rollback()
			return xutils.Error(BANK_DB_ERROR, err)
		}
		tx.Commit()
	}
	return nil
}
func GetUserAnswer(uuid string, paperId string, questionId string) ([]string, error) {
	ansCard, err := tableUser.AnswerCardGetByUserAndPaper(uuid, paperId)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	if ansCard == nil {
		return nil, nil
	}

	if _, exists := ansCard.Answers[questionId]; !exists {
		return nil, nil
	} else {
		return ansCard.Answers[questionId], nil
	}
}

// 保存题库试题笔记
func SetUserQuestionNote(uuid string, paperId, questionId string, note string) error {
	// note:uuid的验证在jwt解密中处理,这里不需要再次检测
	// 检测试卷是否存在
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if paper == nil {
		return xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	// 检测试题是否存在
	ques, err := tableBank.QuestionGetByKey(questionId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if ques == nil {
		return xutils.Error(BANK_INVAILD_QUESTION_ID, "无效的试题ID")
	}

	// 写入笔记
	return tableUser.NoteSetByUserAndPaperAndQuestion(uuid, paperId, questionId, note)
}

// 获取题库试题笔记
func GetUserQuestionNote(uuid string, paperId, questionId string) (string, error) {
	note, err := tableUser.NoteGetByUserAndPaperAndQuestion(uuid, paperId, questionId)
	if err != nil {
		return "", xutils.Error(BANK_DB_ERROR, err)
	}
	if note == nil {
		return "", nil
	}
	return note.Note, nil
}

func SetUserQuestionFavorite(uuid string, paperId string, questionId string, isFavorite bool) error {
	// note:uuid的验证在jwt解密中处理,这里不需要再次检测
	// 检测试卷是否存在
	paper, err := tableBank.PaperGetByKeyAndPublish(paperId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}

	if paper == nil {
		return xutils.Error(BANK_INVAILD_PAPER_ID, "无效的试卷ID")
	}

	// 检测试题是否存在
	ques, err := tableBank.QuestionGetByKey(questionId)
	if err != nil {
		return xutils.Error(BANK_DB_ERROR, err)
	}
	if ques == nil {
		return xutils.Error(BANK_INVAILD_QUESTION_ID, "无效的试题ID")
	}

	// 写入笔记
	return tableUser.FavoriteSetByUserAndPaperAndQuestion(uuid, paperId, questionId, isFavorite)
}

func GetUserFavoriteInfo(uuid string, paperId string, questionId string) (bool, error) {
	infoWithPaper, err := tableUser.FavoriteGetByUserAndPaperAndQuestion(uuid, paperId, questionId)
	if err != nil {
		return false, xutils.Error(BANK_DB_ERROR, err)
	}

	if infoWithPaper == nil {
		return false, nil
	}

	return infoWithPaper.IsFavorite == "1", nil
}

func GetUserFavoriteInfos(uuid string) (*[]FavoriteInfo, error) {

	var favlist []FavoriteInfo

	favs, err := tableUser.FavoriteGetByUser(uuid)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	// 没有记录
	if favs == nil {
		return nil, nil
	}

	var temp = make(map[string]*FavoriteInfo)
	for _, fav := range *favs {
		pa, err := tableBank.PaperGetByKeyAndPublish(fav.PaperID)
		if err != nil || pa == nil {
			continue
		}

		ques, err := GetQuestionInfo(fav.QuestionID)
		if err != nil || ques == nil {
			continue
		}

		if _, exist := temp[fav.PaperID]; !exist {
			favlist = append(favlist, FavoriteInfo{})
			temp[fav.PaperID] = &favlist[len(favlist)-1]
		}

		temp[fav.PaperID].PaperId = fav.PaperID
		temp[fav.PaperID].QuestionId = append(temp[fav.PaperID].QuestionId, fav.QuestionID)
		temp[fav.PaperID].Title = pa.Title
	}
	return &favlist, err
}

func GetUserNoteInfos(uuid string) (*[]NoteInfo, error) {

	var notelist []NoteInfo

	notes, err := tableUser.NoteGetByUser(uuid)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	if notes == nil {
		return nil, nil
	}

	var temp = make(map[string]*NoteInfo)
	for _, note := range *notes {
		pa, err := tableBank.PaperGetByKeyAndPublish(note.PaperID)
		if err != nil || pa == nil {
			continue
		}

		ques, err := GetQuestionInfo(note.QuestionID)
		if err != nil || ques == nil {
			continue
		}

		if _, exist := temp[note.PaperID]; !exist {
			notelist = append(notelist, NoteInfo{})
			temp[note.PaperID] = &notelist[len(notelist)-1]
		}

		temp[note.PaperID].PaperId = note.PaperID
		temp[note.PaperID].Title = pa.Title
		temp[note.PaperID].QuestionId = append(temp[note.PaperID].QuestionId, note.QuestionID)
	}
	return &notelist, err
}

func GetUserWorngInfos(uuid string) (*[]WorngInfo, error) {
	var wrongRecords []WorngInfo

	// 从withAnswer表中获取错题列表
	wrongs, err := tableUser.WrongQuestionGetByUsers(uuid)
	if err != nil {
		return nil, xutils.Error(BANK_DB_ERROR, err)
	}

	if wrongs == nil {
		return nil, nil
	}

	var temp = make(map[string]*WorngInfo)
	// 遍历获取到的错题列表
	for _, wrong := range *wrongs {
		// 获取错题
		ques, err := GetQuestionInfo(wrong.QuestionID)
		if err != nil || ques == nil {
			continue
		}
		// 获取错题的试卷信息
		paper, err := tableBank.PaperGetByKeyAndPublish(wrong.PaperID)
		if err != nil || paper == nil {
			continue
		}
		// 获取错题的答题卡信息
		answer, err := tableUser.AnswerCardGetByID(wrong.AnswerCardID)
		if err != nil || answer == nil {
			continue
		}

		if _, exist := temp[wrong.PaperID]; !exist {
			wrongRecords = append(wrongRecords, WorngInfo{})
			temp[wrong.PaperID] = &wrongRecords[len(wrongRecords)-1]
		}
		temp[wrong.PaperID].PaperId = wrong.PaperID
		temp[wrong.PaperID].Title = paper.Title
		temp[wrong.PaperID].QuestionId = append(temp[wrong.PaperID].QuestionId, wrong.QuestionID)
	}
	return &wrongRecords, err
}
